package com.jai.pms.service.impl;

import java.util.List;
import java.util.Objects;

import com.jai.pms.domain.*;
import com.jai.pms.domain.Process;
import com.jai.pms.mapper.DataMapper;
import com.jai.pms.mapper.ProjectDataMapper;
import com.jai.pms.mapper.ProjectMapper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jai.pms.mapper.ProjectDataCategoryMapper;
import com.jai.pms.service.IProjectDataCategoryService;

/**
 * 项目数据分类Service业务层处理
 * 
 * @author v
 * @date 2023-10-11
 */
@Service
public class ProjectDataCategoryServiceImpl implements IProjectDataCategoryService 
{
    @Autowired
    private ProjectDataCategoryMapper projectDataCategoryMapper;

    @Autowired
    private ProjectDataMapper projectDataMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private DataMapper dataMapper;

    /**
     * 查询项目数据分类
     * 
     * @param id 项目数据分类主键
     * @return 项目数据分类
     */
    @Override
    public ProjectDataCategory selectProjectDataCategoryById(Long id)
    {
        return projectDataCategoryMapper.selectProjectDataCategoryById(id);
    }

    /**
     * 查询项目数据分类列表
     * 
     * @param projectDataCategory 项目数据分类
     * @return 项目数据分类
     */
    @Override
    public List<ProjectDataCategory> selectProjectDataCategoryList(ProjectDataCategory projectDataCategory)
    {
        return projectDataCategoryMapper.selectProjectDataCategoryList(projectDataCategory);
    }

    /**
     * 新增项目数据分类
     * 
     * @param projectDataCategory 项目数据分类
     * @return 结果
     */
    @Override
    public int insertProjectDataCategory(ProjectDataCategory projectDataCategory)
    {
        preCheck(projectDataCategory, true);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectDataCategory.setCreateBy(loginUser.getUsername());
        projectDataCategory.setCreateTime(DateUtils.getNowDate());
        return projectDataCategoryMapper.insertProjectDataCategory(projectDataCategory);
    }

    /**
     * 修改项目数据分类
     * 
     * @param projectDataCategory 项目数据分类
     * @return 结果
     */
    @Override
    public int updateProjectDataCategory(ProjectDataCategory projectDataCategory)
    {
        preCheck(projectDataCategory, false);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectDataCategory.setUpdateBy(loginUser.getUsername());
        projectDataCategory.setUpdateTime(DateUtils.getNowDate());
        return projectDataCategoryMapper.updateProjectDataCategory(projectDataCategory);
    }

    /**
     * 批量删除项目数据分类
     * 
     * @param ids 需要删除的项目数据分类主键
     * @return 结果
     */
    @Override
    public int deleteProjectDataCategoryByIds(Long[] ids)
    {
        return projectDataCategoryMapper.deleteProjectDataCategoryByIds(ids);
    }

    /**
     * 删除项目数据分类信息
     * 
     * @param id 项目数据分类主键
     * @return 结果
     */
    @Override
    public int deleteProjectDataCategoryById(Long id)
    {
        return projectDataCategoryMapper.deleteProjectDataCategoryById(id);
    }

    /**
     * 添加、修改前检查
     *
     * @param projectDataCategory 数据
     * @param isAdd              是否为添加
     * @return 返回结果数据
     */
    private ProjectDataCategory preCheck(ProjectDataCategory projectDataCategory, boolean isAdd) {
        // 检查data是否在project中存在
        ProjectData projectDataQuery = new ProjectData();
        projectDataQuery.setProjectId(projectDataCategory.getProjectId());
        projectDataQuery.setDataId(projectDataCategory.getDataId());
        List<ProjectData> projectDataQueryList = projectDataMapper.selectProjectDataList(projectDataQuery);
        if (projectDataQueryList.size() == 0) {
            throw new ServiceException("该数据在项目中不存在, 无法为其添加分类");
        }

        // 检查是否有重复信息，联合主键projectId和dataId processId
        ProjectDataCategory projectDataCategoryQuery = new ProjectDataCategory();
        projectDataCategoryQuery.setProjectId(projectDataCategory.getProjectId());
        projectDataCategoryQuery.setDataId(projectDataCategory.getDataId());
        projectDataCategoryQuery.setDataCategory(projectDataCategory.getDataCategory());
        List<ProjectDataCategory> queryList = projectDataCategoryMapper.selectProjectDataCategoryList(projectDataCategoryQuery);
        if (isAdd) {
            if (queryList.size() > 0) {
                throw new ServiceException("该数据分类在项目中已经存在");
            }
        } else {
            if (queryList.size() > 0 && !Objects.equals(queryList.get(0).getId(), projectDataCategory.getId())) {
                throw new ServiceException("该数据分类在项目中已经存在");
            }
        }

        // 填充project的code、name信息
        Project project = projectMapper.selectProjectById(projectDataCategory.getProjectId());
        projectDataCategory.setProjectCode(project.getCode());
        projectDataCategory.setProjectName(project.getName());

        // 填充data的code、name信息
        Data data = dataMapper.selectDataById(projectDataCategory.getDataId());
        projectDataCategory.setDataCode(data.getCode());
        projectDataCategory.setDataName(data.getName());

        return projectDataCategory;
    }
}
